using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.Service.IOCExtensions
{
	/// <summary>
	/// This is an extension class for EmployeeTerritory DTO objects and lists
	/// </summary>
	public static partial class EmployeeTerritoryExtension
	{
		#region RunSelect
		/// <summary>
		/// Select all objects from store.
		/// </summary>
		public static void RunSelect(this List<EmployeeTerritoryDTO> list)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			list.AddRange(proxy.RunSelect());
		}
		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a paged result set.
		/// </summary>
		public static PagedQueryResults<EmployeeTerritoryDTO> RunSelect(this List<EmployeeTerritoryDTO> list, int page, int pageSize, bool ascending, string sortColumn, string linq)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			PagedQueryResults<EmployeeTerritoryDTO> retval = proxy.RunSelect(page, pageSize, ascending, sortColumn, linq);
			if (retval.GridData != null)
				list.AddRange(retval.GridData);
			return retval;
		}

		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a result set.
		/// </summary>
		public static void RunSelect(this List<EmployeeTerritoryDTO> list, string linq)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			list.AddRange(proxy.RunSelect(linq));
		}

		#endregion

		#region Persist

		/// <summary>
		/// Persists this object to store.
		/// </summary>
		public static void Persist(this EmployeeTerritoryDTO employeeterritory)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			proxy.Persist(employeeterritory);
		}

		/// <summary>
		/// Persists this collection to store.
		/// </summary>
		public static void Persist(this List<EmployeeTerritoryDTO> employeeterritoryList)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			proxy.Persist(employeeterritoryList);
		}

		#endregion

		#region EmployeeTerritory SelectUsingPK

		/// <summary>
		/// Select a single object from this collection by its primary key.
		/// </summary>
		public static bool SelectByPrimaryKey(this EmployeeTerritoryDTO employeeterritory, int employeeid, string territoryid)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			IEmployeeTerritoryService employeeterritoryService = (IEmployeeTerritoryService)proxy;
			var item = employeeterritoryService.SelectByPrimaryKey(employeeid, territoryid);
			if (item == null) return false;
			CopyObject(item, employeeterritory);
			return true;
		}

		#endregion

		#region EmployeeTerritory Select By Foreign Key
		/// <summary>
		/// Get the related 'Employee' DTO object
		/// </summary>
		public static EmployeeDTO GetEmployee(this EmployeeTerritoryDTO item)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			IEmployeeTerritoryService service = (IEmployeeTerritoryService)proxy;
			return service.GetEmployeeItem(item);
		}

		/// <summary>
		/// Get the related 'Territory' DTO object
		/// </summary>
		public static TerritoryDTO GetTerritory(this EmployeeTerritoryDTO item)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			IEmployeeTerritoryService service = (IEmployeeTerritoryService)proxy;
			return service.GetTerritoryItem(item);
		}


		#endregion

		#region Delete

		/// <summary>
		/// Delete a single object.
		/// </summary>
		public static void Delete(this EmployeeTerritoryDTO employeeterritory)
		{
			var proxy = ProxyFactory<EmployeeTerritoryDTO>.GetProxy();
			IEmployeeTerritoryService employeeterritoryService = (IEmployeeTerritoryService)proxy;
			employeeterritoryService.Delete(employeeterritory);
		}

		#endregion

		/// <summary>
		/// Clones one DTO to another.
		/// </summary>
		internal static void CopyObject(EmployeeTerritoryDTO source, EmployeeTerritoryDTO destination)
		{
			destination.TerritoryId = source.TerritoryId;
			destination.EmployeeId = source.EmployeeId;
		}

	}
}
